<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改认证源')"/>
    <th:block th:include="include :: ztree-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-app-edit" th:object="${app}">
        <input id="appId" name="appId" type="hidden" th:field="*{appId}"/>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">应用名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="appName" id="appName" th:field="*{appName}" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">重定向地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="appUrl" id="appUrl" th:field="*{appUrl}" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">client_id：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="clientId" id="clientId" th:field="*{clientId}" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">client_secret：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="clientSecret" id="clientSecret" th:field="*{clientSecret}" required>
            </div>
        </div>


        <div class="form-group">
            <label class="col-sm-3 control-label is-required">显示顺序：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="sort" id="sort" th:field="*{sort}" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input id="remark" name="remark" class="form-control" type="text" th:field="*{remark}">
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script type="text/javascript">
    var prefix = ctx + "system/app";

    $("#form-app-edit").validate({
        onkeyup: false,
        rules: {
            appName: {
                remote: {
                    url: prefix + "/checkAppNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "appId": function () {
                            return $("#appId").val();
                        },
                        "appName": function () {
                            return $.common.trim($("#appName").val());
                        }
                    }
                }
            },
            clientId: {
                remote: {
                    url: prefix + "/checkClientIdUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "appId": function () {
                            return $("#appId").val();
                        },
                        "clientId": function () {
                            return $.common.trim($("#clientId").val());
                        }
                    }
                }
            },
        },
        messages: {
            "appName": {
                remote: "应用名称已经存在"
            },
            "clientId": {
                remote: "clientId已经存在"
            }
        },
        focusCleanup: true
    });

    function edit() {
        var appId = $("input[name='appId']").val();
        var appName = $("input[name='appName']").val();
        var appUrl = $("input[name='appUrl']").val();
        var clientId = $("input[name='clientId']").val();
        var clientSecret = $("input[name='clientSecret']").val();
        var sort = $("input[name='sort']").val();
        var remark = $("input[name='remark']").val();

        var data = {
            appId,
            appName,
            appUrl,
            clientId,
            clientSecret,
            sort,
            remark
        }


        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/edit",
            data,
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.successCallback(data);
            }
        });
    }

    function submitHandler() {
        if ($.validate.form()) {
            edit();
        }
    }
</script>
</body>
</html>
